package com.hptpd.questionnairesurvey.vo;

import com.google.common.collect.Lists;
import com.hptpd.questionnairesurvey.assets.Constant;
import com.hptpd.questionnairesurvey.domain.Department;
import com.hptpd.questionnairesurvey.utils.AbstractMyBeanUtils;
import lombok.Data;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * \* Created with IntelliJ IDEA.
 * \* Date: 2019/9/9
 * \*
 * \* Description:
 * \
 *
 * @author walter_long
 */
@Data
public class DepartmentVo {
    private Integer id;

    private String name;

    private Long sort;

    private DepartmentVo parentDepartmentVo;

    public static List<DepartmentVo> convertDepartment(List<Department> departments) {
        List<DepartmentVo> departmentVos = Lists.newLinkedList();
        for (Department department : departments) {
            departmentVos.add(convertDepartment(department));
        }
        return departmentVos;
    }

    public static DepartmentVo convertDepartment(Department department) {
        if (null == department) {
            return null;
        }
        DepartmentVo departmentVo = new DepartmentVo();
        AbstractMyBeanUtils.copyProperties(department, departmentVo);
        if (null == department.getParentDepartment()) {
            departmentVo.setParentDepartmentVo(null);
        } else {
            DepartmentVo parentDepartmentVo = new DepartmentVo();
            AbstractMyBeanUtils.copyProperties(department.getParentDepartment(), parentDepartmentVo);
            departmentVo.setParentDepartmentVo(parentDepartmentVo);
        }
        return departmentVo;
    }
    
    public static Department getRootDepartment(@NotNull Department department) {
        return getParentDepartment(department);
    }

    private static Department getParentDepartment(@NotNull Department department) {
        if (department.getParentDepartment().getId() == Constant.ROOT_DEPARTMENT_ID) {
            return department;
        }
        return getParentDepartment(department.getParentDepartment());
    }
}
